import request from '@/utils/request';
import { DeviceListItem, DeviceGroupListItem } from './data.d';
import qs from 'qs';

export async function queryRule(params?: DeviceGroupListItem) {
  return request('/api/rule', {
    params,
  });
}

const getRandomuserParams = (params: any) => {
  return {
    pageNo: params.current,
    ...params,
  };
};
//设备列表
export async function deviceList(params?: DeviceListItem) {
  params = getRandomuserParams(params);
  const resolution = params?.resolution;
  if(resolution && resolution.indexOf("x")>-1){
    const resolutionWidth = resolution.split("x")[0];
    const resolutionHeight = resolution.split("x")[1];
    if(resolutionWidth){
      params["width"] = resolutionWidth;
    }
    if(resolutionHeight){
      params["height"] = resolutionHeight;
    }
    params["resolution"] = undefined;
  }
  const res = request('/api/device/list/listPage', {
    headers: {
      Authorization: localStorage.getItem('token'),
    },
    params,
  });
  return res.then(function (response) {
    if (response && response.code === 200) {
      //数据转换
      const obj = new Object();
      let list = response.data.list;
      for (var i in list) {
        list[i].index = i;
        list[i].key = list[i].id;
        list[i].resolution = list[i].width+'x'+list[i].height;
        if(list[i].registerStatus){
          list[i].registerStatus = "true";
        }else{
          list[i].registerStatus = "false";
        }
      }
      obj['data'] = response.data.list;
      obj['pageCount'] = response.data.pageCount;
      obj['current'] = response.data.pageNo;
      obj['total'] = response.data.totalCount;
      return obj;
    }
  });
}
//导出设备列表
export async function exportDeviceList(params?: DeviceListItem) {
  params = getRandomuserParams(params);
  const resolution = params?.resolution;
  if(resolution && resolution.indexOf("x")>-1){
    const resolutionWidth = resolution.split("x")[0];
    const resolutionHeight = resolution.split("x")[1];
    if(resolutionWidth){
      params["width"] = resolutionWidth;
    }
    if(resolutionHeight){
      params["height"] = resolutionHeight;
    }
    params["resolution"] = undefined;
  }
  return request('/api/device/list/listPage', {
    headers: {
      Authorization: localStorage.getItem('token'),
    },
    params,
  });
}
//添加设备
export async function addDevice(params: any) {
  return request('/api/device/list/addDevice', {
    method: 'POST',
    headers: {
      Authorization: localStorage.getItem('token'),
    },
    data: {
      ...params,
      method: 'update',
    },
  });
}

//删除设备
export async function removeDevice(id: number) {
  return request(`/api/device/list/deleteDevice/${id}`, {
    headers: {
      Authorization: localStorage.getItem('token'),
    },
    method: 'delete',
  });
}

//设备重命名
export async function renameDevice(params: DeviceListItem) {
  return request(`/api/device/list/changeDeviceName/${params.id}?name=${params.name}`, {
    method: 'PUT',
    headers: {
      Authorization: localStorage.getItem('token'),
    },
  });
}

//设备修改机构
export async function changeDeviceOrg(params: DeviceListItem) {
  return request(`/api/device/list/changeDeviceOrg/${params.id}?orgId=${params.orgId}`, {
    method: 'PUT',
    headers: {
      Authorization: localStorage.getItem('token'),
    },
  });
}
//设备数据导出
export async function deviceExportData(params: any) {
  params = getRandomuserParams(params);
  return request(`/api/device/list/exportDataWithIds?ids=10026`, {
    method: 'GET',
    headers: {
      Authorization: localStorage.getItem('token'),
      'responseType':'blob',
    },
    // params,
  });
}
//分配标签
export async function setTag(params: any) {
  return request('/api/device/list/setTag', {
    method: 'PUT',
    headers: {
      Authorization: localStorage.getItem('token'),
      'Content-Type': 'application/x-www-form-urlencoded',
    },
    data: qs.stringify(params, { arrayFormat: 'indices', allowDots: true }),
  });
}
//设备详情
export async function deviceDetail(deviceId: number,signal:AbortSignal) {
  return request(`/api/device/list/deviceDetail/${deviceId}`, {
    signal: signal,
    method: 'GET',
    headers: {
      Authorization: localStorage.getItem('token'),
    },
  });
}
//设备控制--获取设备信息
export async function devicePropForControl(deviceId: number) {
  return request(`/api/device/list/devicePropForControl/${deviceId}`, {
    method: 'GET',
    headers: {
      Authorization: localStorage.getItem('token'),
    },
  });
}

//设备控制--设备截屏
export async function deviceControlForPrintScreen(mac: string) {
  return request(`/api/device/list/deviceControlForPrintScreen/${mac}`, {
    method: 'POST',
    headers: {
      Authorization: localStorage.getItem('token'),
      'Content-Type': 'application/x-www-form-urlencoded',
    }
  });
}
//设备控制--定时开关机
export async function deviceControlForTimerSwitch(mac:string,params: any) {
  return request(`/api/device/list/deviceControlForTimerSwitch/${mac}`, {
    method: 'PUT',
    headers: {
      Authorization: localStorage.getItem('token'),
    },
    data: {
      ...params,
      method: 'put',
    },
  });
}
//设备控制--重启设备
export async function deviceControlForReboot(mac: string) {
  return request(`/api/device/list/deviceControlForReboot/${mac}`, {
    method: 'PUT',
    headers: {
      Authorization: localStorage.getItem('token'),
      'Content-Type': 'application/x-www-form-urlencoded',
    },
  })
}
//设备控制--音量控制
export async function deviceControlForVolume(mac:string,params: any) {
  return request(`/api/device/list/deviceControlForVolume/${mac}`, {
    method: 'PUT',
    headers: {
      Authorization: localStorage.getItem('token'),
      'Content-Type': 'application/x-www-form-urlencoded',
    },
    data: qs.stringify(params, { arrayFormat: 'indices', allowDots: true }),
  })
}
//设备控制--亮度控制
export async function deviceControlForBackLight(mac:string,params: any) {
  return request(`/api/device/list/deviceControlForBackLight/${mac}`, {
    method: 'PUT',
    headers: {
      Authorization: localStorage.getItem('token'),
      'Content-Type': 'application/x-www-form-urlencoded',
    },
    data: qs.stringify(params, { arrayFormat: 'indices', allowDots: true }),
  })
}
//设备控制--系统升级
export async function deviceControlForSystemUpgrade(mac: string, params: any) {
  return request(`/api/device/list/deviceControlForSystemUpgrade/${mac}`, {
    method: 'PUT',
    headers: {
      Authorization: localStorage.getItem('token'),
      'Content-Type': 'application/x-www-form-urlencoded',
    },
    data: qs.stringify(params, { arrayFormat: 'indices', allowDots: true }),
  })
}
//设备控制--清除缓存
export async function deviceControlForCleanCache(mac: string) {
  return request(`/api/device/list/deviceControlForCleanCache/${mac}`, {
    method: 'PUT',
    headers: {
      Authorization: localStorage.getItem('token'),
      'Content-Type': 'application/x-www-form-urlencoded',
    },
  })
}
//设备分组列表
export async function deviceGroupList(params?: DeviceGroupListItem) {
  params = getRandomuserParams(params);
  const res = request('/api/device/group/list', {
    headers: {
      Authorization: localStorage.getItem('token'),
    },
    params,
  });
  return res.then(function (response) {
    if (response && response.code === 200) {
      //数据转换
      const obj = new Object();
      let list = response.data.list;
      for (var i in list) {
        list[i].index = parseInt(i)+1;//序号
        list[i].key = list[i].id;
      }
      obj['data'] = response.data.list;
      obj['pageCount'] = response.data.pageCount;
      obj['current'] = response.data.pageNo;
      obj['total'] = response.data.totalCount;
      return obj;
    }
  });
}

//添加设备分组
export async function addDeviceGroup(params: DeviceGroupListItem) {
  return request('/api/device/group/add', {
    method: 'POST',
    headers: {
      Authorization: localStorage.getItem('token'),
    },
    data: {
      ...params,
      method: 'post',
    },
  });
}
//编辑设备分组
export async function updateDeviceGroup(params: DeviceGroupListItem) {
  return request('/api/device/group/updateGroup', {
    method: 'PUT',
    headers: {
      Authorization: localStorage.getItem('token'),
    },
    data: {
      ...params,
      method: 'put',
    },
  });
}
//删除设备分组
export async function removeDeviceGroup(id: number) {
  return request(`/api/device/group/deleteGroup/${id}`, {
    headers: {
      Authorization: localStorage.getItem('token'),
    },
    method: 'delete',
  });
}
//分组详情
export async function deviceGroupDetail(params: any) {
  params = getRandomuserParams(params);
  return request('/api/device/group/groupDetail', {
    method: 'GET',
    headers: {
      Authorization: localStorage.getItem('token'),
    },
    params,
  })
  // .then((response) => {
  //   if (response && response.code === 200) {
  //     return response;
  //   }
  // });
}

